package com.cytech.cloudclinic.moudle.main.drug.add;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.cytech.cloudclinic.R;
import com.cytech.cloudclinic.base.BaseActivity;
import com.cytech.cloudclinic.base.BasePresenter;
import com.cytech.cloudclinic.moudle.main.drug.add.base.AddDrugBaseFragment;
import com.cytech.cloudclinic.moudle.main.drug.add.dosage.AddDrugDosageFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 新增药品
 *
 * @author fangdingjie
 */
public class AddDrugActivity extends BaseActivity {

    private static final String TAG = "AddDrugActivity";
    @BindView(R.id.rg_add_drug)
    RadioGroup radioGroup;
    @BindView(R.id.rb_add_drug_base)
    RadioButton rbAddDrugBase;
    @BindView(R.id.rb_add_drug_dosage)
    RadioButton rbAddDrugDosage;

    private int currentFragmentIndex = 0;
    private List<Fragment> fragmentList;
    private Fragment baseInfoFragment;
    private Fragment dosageFragment;
    private Fragment currentFragment;

    /**
     * @param context
     * @param drugTagId 药品2级分类id
     * @param drugId    药品id，新增为0，修改为药品id
     * @param parentDrugTagId 药品父类id，在添加标签的时候使用，添加标签统一添加到父标签下面
     */
    public static void actionStart(Context context, int drugTagId,int drugId,int parentDrugTagId) {
        Intent intent = new Intent(context, AddDrugActivity.class);
        intent.putExtra("drugTagId", drugTagId);
        intent.putExtra("drugId",drugId);
        intent.putExtra("parentDrugTagId",parentDrugTagId);
        context.startActivity(intent);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.act_add_drug;
    }

    @Override
    protected void initView() {
        //药品分类id
        int drugTagId = getIntent().getIntExtra("drugTagId", 0);
        //药品id
        int drugId = getIntent().getIntExtra("drugId", 0);
        //药品父分类id
        int parentTagId = getIntent().getIntExtra("parentDrugTagId", 0);
        Log.d(TAG, "drugTagId="+drugTagId+"   drugId="+drugId);

        fragmentList = new ArrayList<>();
        baseInfoFragment = AddDrugBaseFragment.newInstance(drugTagId,drugId,parentTagId);
        dosageFragment = AddDrugDosageFragment.newInstance(drugTagId,drugId,parentTagId);
        fragmentList.add(baseInfoFragment);
        fragmentList.add(dosageFragment);

        switchFragment(0);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.rb_add_drug_base:
                        switchFragment(0);
                        break;
                    case R.id.rb_add_drug_dosage:
                        switchFragment(1);
                        break;
                }
            }
        });
    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.titlebar_back)
    public void exit(View v) {
        finish();
    }

    private void switchFragment(int position) {
        currentFragmentIndex = position;
        Fragment targetFragment = fragmentList.get(position);
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        if (!targetFragment.isAdded()) {
            if (currentFragment != null) {
                transaction.hide(currentFragment);
            }
            transaction.add(R.id.fl_add_drug_container, targetFragment).commit();
            //System.out.println("还没添加呢");
        } else {
            transaction
                    .hide(currentFragment)
                    .show(targetFragment)
                    .commit();
            //System.out.println("添加了( ⊙o⊙ )哇");
        }
        currentFragment = targetFragment;
    }

    public Fragment getDosageFragment() {
        return dosageFragment;
    }

    //切换下一步
    public void setRadioGroupSelect() {
        rbAddDrugBase.setChecked(false);
        rbAddDrugDosage.setChecked(true);
    }


}
